package com.example.smallmall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.smallmall.common.api.CommonResult;
import com.example.smallmall.entity.SpAds;
import com.example.smallmall.service.ISpAdsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author WSGL
 * @since 2020-08-19
 */
@Api(tags = "广告管理")
@RestController
@RequestMapping("/Ads")
public class SpAdsController {

    @Resource
    private ISpAdsService iSpAdsService;

    @ApiOperation("首页轮播图")
    @RequestMapping(value = "/Carousel/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<SpAds>> getCarousel(@PathVariable("id") @ApiParam("位置id") Integer id) {
        QueryWrapper<SpAds> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SpAds::getLocationId, id);
        List<SpAds> list = iSpAdsService.list(queryWrapper);
        if (list != null) {
            return CommonResult.success(list);
        } else {
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("广告列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Map<String, Object>> listAds(@RequestParam(value = "pageNum", defaultValue = "1") @ApiParam("页码") Integer pageNum,
                                                     @RequestParam(value = "pageSize", defaultValue = "5")
                                                     @ApiParam("每页数量") Integer pageSize) {
        Page<Map<String, Object>> page = new Page<>(pageNum, pageSize);
        Page<Map<String, Object>> mapPage = iSpAdsService.pageMaps(page);
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("ads", mapPage.getRecords());
        objectMap.put("total", mapPage.getTotal());
        objectMap.put("pageNum", mapPage.getCurrent());
        if (!objectMap.isEmpty()) {
            return CommonResult.success(objectMap);
        } else {
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("添加广告信息")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<SpAds> createAds(@RequestBody SpAds spAds) {
        boolean save = iSpAdsService.save(spAds);
        if (save) {
            return CommonResult.success(spAds);
        } else {
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("编辑广告信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public CommonResult<SpAds> updateAds(@PathVariable("id") Integer id, @RequestBody SpAds spAds) {
        UpdateWrapper<SpAds> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SpAds::getAdsId, id);
        boolean update = iSpAdsService.update(spAds, updateWrapper);
        if (update) {
            return CommonResult.success(spAds);
        } else {
            return CommonResult.failed("操作失败");
        }
    }
}
